<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>新增学院</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css"/>
</head>
<body>
<form class="layui-form">
    <div class="mainBox">
        <div class="main-container">
            <div class="main- container">
                <input id="classId" type="hidden" name="classId">
                <div class="layui-form-item">
                    <label class="layui-form-label">班级名称</label>
                    <div class="layui-input-block">
                        <input id="classes" type="text" name="classes" lay-verify="required" autocomplete="off"
                               placeholder="请输入班级名称"
                               class="layui-input" lay-reqText="请输入班级名称">
                    </div>
                </div>

                <!--专业-->
                <div class="layui-form-item">
                    <label class="layui-form-label">所属专业</label>
                    <div class="layui-input-block">
                        <select id="major" name="major" lay-verify="required"
                                placeholder="请输入所属专业名称"
                                lay-search lay-reqText="请选择班级所属专业">
                        </select>
                    </div>
                </div>

                <!--辅导员-->
                <div class="layui-form-item">
                    <label class="layui-form-label">直属辅导员</label>
                    <div class="layui-input-block">
                        <select id="counselor" name="counselor" lay-verify="required"
                                lay-search lay-reqText="请选择直属辅导员">
                        </select>
                    </div>
                </div>

            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
        </div>
    </div>
</form>
<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script>
    layui.use(['form', 'jquery', 'loading', 'laydate'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let loading = layui.loading;

        //动态获取教师列表
        $.ajax({
            url: '/ClassInfo/getTeacherList',
            dataType: 'json',
            type: 'post',
            success: function (res) {
                //console.log(res.data);
                let counselor = $('#counselor');
                counselor.append('<option value="">请选择直属辅导员</option>')
                for (let i = 0; i < res.data.length; i++) {
                    let counselorName = res.data[i].realName;
                    let teacherId = res.data[i].teacherId;
                    counselor.append('<option value="' + teacherId + '">' + counselorName
                        + '(' + teacherId + ')' + '</option>')
                }
                form.render('select');
            }
        })

        //动态获取专业列表
        $.ajax({
            url: '/Major/getMajors',
            dataType: 'json',
            type: 'post',
            success: function (res) {
                console.log(res.data);
                let major = $('#major');
                major.append('<option value="">请选择专业</option>')
                for (let i = 0; i < res.data.length; i++) {
                    let majorName = res.data[i].majorName;
                    major.append('<option value="' + majorName + '">' + majorName + '</option>')
                }
                form.render('select');
            }
        })

        form.on('submit(user-save)', function (data) {
            //console.log(data.field);
            loading.Load(3, "");
            $.ajax({
                url: '/ClassInfo/insertClassInfo',
                data: data.field,
                dataType: 'json',
                type: 'post',
                success: function (result) {
                    loading.loadRemove();
                    console.log(result);
                    if (result.success == '1') {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            //console.log(JSON.stringify(result.data));
                            parent.layui.table.reload("user-table", {
                                data: result.data,
                                page: {
                                    layout: ['count', 'prev', 'page', 'next', 'skip']
                                    , curr: parent.layui.jquery(".layui-laypage-em").next().html()
                                    , groups: 3
                                    , last: false
                                }
                            });
                        });
                    } else {
                        layer.open({
                            title: '提示'
                            , icon: 2
                            , content: result.msg
                        });
                    }
                }
            })
            return false;
        });
        //前端实时检测用户是否存在
        $('#collegeId').blur(function () {
            if ($('#collegeId').val() == '') {
                return false;
            }
            $('#userIdTips').append('<i class="tipsLoad layui-icon layui-icon-loading layui-anim layui-anim-rotate ' +
                'layui-anim-loop"></i>');
            $.ajax({
                url: '/College/getCollegeById',
                data: {
                    collegeId: $('#collegeId').val()
                },
                dataType: 'json',
                type: 'get',
                success: function (result) {
                    $('.tipsLoad').remove();
                    console.log(result);
                    if (result.count <= 0) {
                        $('#userIdTips').hide();
                    } else {
                        $('#userIdTips').show();
                    }
                },
                error: function (result) {
                    console.log(result);
                }
            })
        })
    })
</script>
<script>
</script>
</body>
</html>